package com.hrm.dao.Impl;

import java.util.List;

import com.hrm.dao.NoticeDao;
import com.hrm.domain.Notice;
import com.hrm.domain.NoticeExt;
import com.hrm.utils.JDBCTools;

/*
 * 公告数据处理层实现类
 */
@SuppressWarnings("all") // 使用注解抑制警告信息
public class NoticeDaoImpl implements NoticeDao {

	// 定义全局变量
	private Class nClass = Notice.class;
	private Class neClass = NoticeExt.class;

	// 分页查询 + 模糊
	@Override
	public List<NoticeExt> getNoticeExtsByTerm(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 没输入查询条件
			sql = "select n.`ID` noticeID, n.`TITLE` title, n.`CONTENT` content, n.`CREATE_DATE` noticeCreateDate, n.`USER_ID` nUserID ,u.username nUserName from `notice_inf` n join user_inf u on n.USER_ID=u.ID limit ?,?;";
		} else if (flag == 2) {// 输入了查询条件 包括用户ID
			sql = "select n.`ID` noticeID, n.`TITLE` title, n.`CONTENT` content, n.`CREATE_DATE` noticeCreateDate, n.`USER_ID` nUserID ,u.username nUserName from `notice_inf` n join user_inf u on n.USER_ID=u.ID where n.TITLE like ? and n.CONTENT like ? and n.`USER_ID`=? limit ?,?;";
		} else {// 输入了查询条件 不包括用户ID
			sql = "select n.`ID` noticeID, n.`TITLE` title, n.`CONTENT` content, n.`CREATE_DATE` noticeCreateDate, n.`USER_ID` nUserID ,u.username nUserName from `notice_inf` n join user_inf u on n.USER_ID=u.ID where n.TITLE like ? and n.CONTENT like ? limit ?,?;";
		}
		return JDBCTools.dataQuery(neClass, sql, parametes);
	}

	// 获取查询结果的记录条数
	@Override
	public Integer getCount(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 没输入查询条件
			sql = "select count(*) from `notice_inf` n join user_inf u on n.USER_ID=u.ID;";
		} else if (flag == 2) { // 输入了查询条件 包括用户ID
			sql = "select count(*) from `notice_inf` n join user_inf u on n.USER_ID=u.ID where n.TITLE like ? and n.CONTENT like ? and n.`USER_ID`=?;";
		} else { // 输入了查询条件 不包括用户ID
			sql = "select count(*) from `notice_inf` n join user_inf u on n.USER_ID=u.ID where n.TITLE like ? and n.CONTENT like ?;";
		}
		return JDBCTools.dataCount(sql, parametes);
	}

	// 添加公告
	@Override
	public Integer addNotice(Notice notice) {
		String sql = "INSERT INTO `notice_inf` (`TITLE`, `CONTENT`, `USER_ID`) VALUES (?,?,?);";
		return JDBCTools.dataUpdate(sql, notice.getTitle(), notice.getContent(), notice.getnUserID());
	}

	// 根据公告标题查找公告
	@Override
	public Notice getNoticeByNN(String noticeName) {
		String sql = "select `ID` noticeID, `TITLE` title, `CONTENT` content, `CREATE_DATE` noticeCreateDate, `USER_ID` nUserID from `notice_inf` where `TITLE` = ?;";
		Notice notice = null;
		List<Notice> notices = JDBCTools.dataQuery(nClass, sql, noticeName);
		if (!notices.isEmpty()) {
			notice = notices.get(0);
		}
		return notice;
	}

	// 根据公告ID获取指定公告
	@Override
	public Notice getNoticeByID(int ID) {
		String sql = "select `ID` noticeID, `TITLE` title, `CONTENT` content, `CREATE_DATE` noticeCreateDate, `USER_ID` nUserID from `notice_inf` where `ID` = ?;";
		Notice notice = null;
		List<Notice> notices = JDBCTools.dataQuery(nClass, sql, ID);
		if (!notices.isEmpty()) {
			notice = notices.get(0);
		}
		return notice;
	}

	// 根据公告ID删除指定公告
	@Override
	public Integer delNotice(int ID) {
		String sql = "delete from `notice_inf` where `ID` = ?;";
		return JDBCTools.dataUpdate(sql, ID);
	}

	// 根据公告ID修改指定的公告信息
	@Override
	public Integer modifyNoticeByID(Notice newNotice, int ID) {
		String sql = "UPDATE `notice_inf` SET `TITLE` = ?, `CONTENT` = ? WHERE `ID` = ?;";
		return JDBCTools.dataUpdate(sql, newNotice.getTitle(), newNotice.getContent(), ID);
	}

}
